<?php
/**
 * Created by PhpStorm.
 * User: cc
 * Date: 2023/10/16
 * Time: 3:16 PM
 */

namespace app\zyhsapi\model;

use app\common\lib\exception\ApiException;
use XznsTools\cache\zyhs\CacheManageZyhs;

class ZyhsAppNotice extends BaseCacheZyhs
{
    protected $table = 'good_wechatadmin.zyhs_app_notice';

    protected $cacheModel = 'config.notice';

    /**
     * 获取最新公告数据
     * @return array
     * @throws \Exception
     */
    public function getNewestNotice()
    {
        $header = request()->header();
        $key = $header['app-type'] . 'V' . $header['version'];
        $ids = CacheManageZyhs::getInstance()->setCacheConfig($this->cacheModel, $key)->ZsGetIocData('list', function () use ($header)
        {
            $where = ['sv.type' => 2, 'v.type' => $header['app-type'], 'v.version_name' => 'V' . $header['version'], 'n.status' => 1];
            try
            {
                $ids = $this
                    ->alias('n')
                    ->where($where)
                    ->order('id', 'DESC')
                    ->join('good_wechatadmin.zyhs_banner_version sv', 'n.id=sv.sid')
                    ->join('good_wechatadmin.zyhs_app_version v', 'sv.vid=v.id')
                    ->column('n.id');
                return array_flip_key_value($ids);
            }
            catch (\Exception $e)
            {
                return [];
            }
        });
        $data = $this->batchGetInfoByIds($ids);
        if ($data)
        {
            return $this->noticeDataReset($ids, $data);
        }
        return [];
    }

    /**
     * 公告框数据重组
     * @param $ids
     * @param $notice
     * @return array
     */
    public function noticeDataReset($ids, $notice)
    {
        if ($ids)
        {
            $id = max($ids);//最新公告id
        } else
        {
            return [];
        }
        //公告为jpg或gif时算出向上偏移比例【算法：（图片居中时距上的高度 - 蓝狐所给距上的高度）/ 模板或手机的高度】保留两位
        if (in_array((int)$notice[$id]['img_type'], [2, 3], true))
        {
            $imgTopHeight = -0.07;//图片距上高度比例 当img_type为2，3时存在，否则不存在,负数是向上偏移，正数是向下偏移
        }
        return [
            'id' => (int)$id,
            'type' => (int)$notice[$id]['type'],
            'img_url' => $notice[$id]['img_url'],
            'img_width' => (int)$notice[$id]['img_width'],
            'img_height' => (int)$notice[$id]['img_height'],
            'img_type' => (int)$notice[$id]['img_type'],
            'title' => $notice[$id]['title'],
            'des' => $notice[$id]['des'],
            'content' => $notice[$id]['content'],
            'btn' => $notice[$id]['btn'],
            'target' => $notice[$id]['target'],
            'link' => htmlspecialchars_decode($notice[$id]['link']),
            'sep_day' => (int)$notice[$id]['sep_day'],
            'img_top_height' => $imgTopHeight ?? 0
        ];
    }

    /**
     * 数据库批量获取公告信息
     * @param array $ids
     * @return array
     * @throws ApiException
     */
    public function getInfoByIdsInData($ids, $type, $sortType)
    {
        $field = 'id,type,img_url,img_width,img_height,img_type,title,des,content,btn,target,link,sep_day,young_model';
        try
        {
            $data = $this
                ->field($field)
                ->where(['id' => ['in', $ids]])
                ->column($field, 'id');
        }
        catch (\Exception $e)
        {
            throw new ApiException('公告信息获取失败', 200, config('code.error'));
        }
        return $data;
    }
}
